<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class AdminLoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;
	public $isSuperAdmin;

	private $_identity;
	private $_returnUrl;


	/**
	 * Declares the validation rules.
	 * The rules state that mobile and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// mobile and password are required\ 
			array('username', 'required','message'=>'用户名不能为空'), 
			array('password', 'required','message'=>'密码不能为空'), 
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// isSuperAdmin needs to be a boolean
			array('isSuperAdmin', 'boolean'),
			// password needs to be authenticated
		    array('password', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'username'=>'用户名',
			'password'=>'密码',
			'rememberMe'=> '是否记着我',
			'isSuperAdmin'=>'系统管理员',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			// $this->_identity=new UserIdentity($this->username,$this->password);
			// if(!$this->_identity->authenticate())
			// 	$this->addError('password','Incorrect mobile or password.');
			//判断是否选择isSuperAdmin单选框,如果选择使用admin表来查询，
        	//没有选择的话使用企业管理员表查询 
        	 
       		 $this->_identity  =  new UserIdentity($this->username,md5($this->password.Yii::app()->params['private_key']));
	         $errorCode="";
	        if($this->isSuperAdmin){
	            $admin = Admin::model()->find("username=:username" , array(':username'=>$this->username)); 

	            if(empty($admin)){
	            	 $errorCode="1";
	            }elseif($admin->password !== md5($this->password.Yii::app()->params['private_key'])){
	            	 $errorCode="1";
	            } 
	         
	            if(!empty($errorCode)){
					 $this->addError('password','用户名或密码错误,请检查.');
	            }else{
	            	 $this->_identity->setId($admin->id); 
	            	 $this->_returnUrl="?r=admin";
	            } 
	        }else{
 				$company = Company::model()->find("username=:username" , array(':username'=>$this->username)); 
 				if(empty($company)){
	            	 $errorCode="1";
	            }elseif($company->password !== md5($this->password.Yii::app()->params['private_key'])){
	            	 $errorCode="1";
	            } 
	            if(!empty($errorCode)){
					 $this->addError('password','用户名或密码错误,请检查.');
	            }else{
	            	 $this->_identity->setId($company->id); 
	            	 $this->_returnUrl="?r=company/view";
	            } 
	        }
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		 Yii::app()->user->returnUrl = $this->_returnUrl;
		 Yii::app()->user->login($this->_identity,0);
		 return true;
		 
		// if($this->_identity===null)
		// {
		// 	$this->_identity=new UserIdentity($this->username,$this->password);
		// 	$this->_identity->authenticate();
		// }
		// if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		// {
		// 	$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
		// 	Yii::app()->user->login($this->_identity,$duration);
		// 	return true;
		// }
		// else
		// 	return false;
	}
}
